﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Experimentor
{
    public class UserdefFunction
    {
        private string _name;
        private UserdefFunctionType _type;
        private string _functioncode;

        public UserdefFunction()
        {
            this._name = "";
            this._type = new UdefFuncType_EnterSequence();
            this._functioncode = "";
        }

        public string Name
        {
            get { return this._name; }
            set { this._name = value; }
        }

        public string Functiontype
        {
            get { return this._type.GetType().Name; }
            set
            {
                try
                {
                    Type functype = Type.GetType("Experimentor." + value);
                    this._type = Activator.CreateInstance(functype) as UserdefFunctionType;
                }
                catch (Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show("FATAL: " + ex.Message, "Fatal Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                    this._type = new UdefFuncType_EnterSequence();
                }
            }
        }

        public UserdefFunctionType getFunctiontypeInstance()
        {
            return this._type;
        }

        public string Code
        {
            get { return this._functioncode; }
            set { this._functioncode = value; }
        }
    }

    public abstract class UserdefFunctionType
    {
        protected string _name;
        protected string _description;
        protected string _returntype;
        protected string _params;
        
        public string Name
        {
            get { return this._name; }
            private set { this._name = value; }
        }

        public string Description
        {
            get { return this._description; }
            private set { this._description = value; }
        }

        public string Returntype
        {
            get { return this._returntype; }
            private set { this._returntype = value; }
        }

        public string Params
        {
            get { return this._params; }
            private set { this._params = value; }
        }
    }

    public class UdefFuncType_EnterSequence : UserdefFunctionType
    {
        public UdefFuncType_EnterSequence()
        {
            this._name = "Enter sequence";
            this._description = "User defined function that returns bool value if sequence should be entered";
            this._returntype = "bool";
            this._params = "DatasaveObject data";
        }
    }

    public class UdefFuncType_EndSequence : UserdefFunctionType
    {
        public UdefFuncType_EndSequence()
        {
            this._name = "End sequence";
            this._description = "User defined function for calculation of results after sequence";
            this._returntype = "void";
            this._params = "DatasaveObject data";
        }
    }

    public class UdefFuncType_SetTime : UserdefFunctionType
    {
        public UdefFuncType_SetTime()
        {
            this._name = "Set presentation time";
            this._description = "User defined function to set the presentation time of slide";
            this._returntype = "int";
            this._params = "DatasaveObject data";
        }
    }
}
